package com.example.tplus.controller;

import com.example.tplus.bean.HttpData;
import com.example.tplus.bean.InSertQCBeam;
import com.example.tplus.bean.QCBean;
import com.example.tplus.bean.QueryQCBean;
import com.example.tplus.mapper.QcMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class WanShengQcController {
    @Autowired
    QcMapper qcMapper;

    Logger logger = LoggerFactory.getLogger(getClass());

    @RequestMapping(value = "getSubmitted")
    @ResponseBody
    public HttpData<List<Map<String, QCBean>>> getSubmitted(@RequestBody QueryQCBean queryQCBean) {
        logger.info(queryQCBean.toString());
        HttpData<List<Map<String, QCBean>>> httpData = new HttpData<>();
        List<Map<String, QCBean>> qcBeanList = new ArrayList<>();
        Map<String, QCBean> qcBeanMap = new HashMap<>();
        //当前工序，未报工
        if (qcMapper.getCurrentBaoGong(queryQCBean) != null && qcMapper.getCurrentBaoGong(queryQCBean).size() > 0) {
            QCBean current = qcMapper.getCurrentBaoGong(queryQCBean).get(0);
            qcBeanMap.put("current", current);
            qcBeanList.add(qcBeanMap);
            logger.info("当前工序：" + current.toString());
            //上道工序，已报工
            if (qcMapper.getBaoGong(queryQCBean) != null && qcMapper.getBaoGong(queryQCBean).size() > 0) {
                QCBean last = qcMapper.getBaoGong(queryQCBean).get(0);
                qcBeanMap.put("last",last);
                qcBeanList.add(qcBeanMap);
                logger.info("上一道工序：" + last.toString());
            }
            httpData.setCode(0);
            httpData.setData(qcBeanList);
            return httpData;
        } else if (qcMapper.getBaoGong(queryQCBean) != null && qcMapper.getBaoGong(queryQCBean).size() > 0) {
            //全部报完工，没有当前工序了，就查出上道已报的工序
            //上道工序，已报工
            QCBean last = qcMapper.getBaoGong(queryQCBean).get(0);
            qcBeanMap.put("last",last);
            qcBeanList.add(qcBeanMap);
            logger.info("上一道工序,无当前工序：" + last.toString());
            httpData.setCode(0);
            httpData.setData(qcBeanList);
            return httpData;
        } else {
            httpData.setCode(1);
            httpData.setMsg("未查询到该订单信息");
            return httpData;
        }
    }

    @RequestMapping(value = "insertQC")
    @ResponseBody
    public HttpData insertQC(@RequestBody InSertQCBeam inSertQCBeam) {
        logger.info(inSertQCBeam.toString());
        HttpData httpData = new HttpData();
        if (qcMapper.insertQC(inSertQCBeam.getQcBean()) > 0) {
            httpData.setCode(0);
        } else {
            httpData.setCode(1);
            httpData.setMsg("质检失败，请联系管理员");
        }
        return httpData;
    }
}
